/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt.core.ui;

import com.gadglet.client.gwt.core.GadgletQuery;
import com.gadglet.client.gwt.core.GadgletRequest;
import com.gadglet.client.gwt.core.GadgletResponse;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.gadglet.client.gwt.ui.UIUtils;
import com.gadglet.params.ProfPrivacyStatus;
import com.gadglet.params.ProfileFields;
import com.gadglet.params.ReqActionTypes;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;



/**
 * UI panel for existing invitations sent to the user
 *
 */
public class InvitationsPanel {
	DebugDialogBox debug = DebugDialogBox.getErrorNotifier();

	protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();

	private final VerticalPanel invitationsContainer = new VerticalPanel();
	
	private final VerticalPanel invitationsList = new VerticalPanel();
	

	final GadgletQuery viewInvitationsQry = new GadgletQuery();

	final ViewInvitationsRequest viewInvitationsRequest = new ViewInvitationsRequest(
			viewInvitationsQry);

	final GadgletQuery invitationsActionQry = new GadgletQuery();

	final InvitationsActionRequest invitationsActionRequest = new InvitationsActionRequest(
			invitationsActionQry);

	private static InvitationsPanel invitationsPanel = null;

	public VerticalPanel getInvitationsContainer() {
		return this.invitationsContainer;
	}

	public static InvitationsPanel getInvitationsPanel() {
		if (invitationsPanel == null) {
			invitationsPanel = new InvitationsPanel();
		}
		return invitationsPanel;
	}

	InvitationsPanel() {
		invitationsContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Label invitationsLabel = new Label();
		invitationsLabel.setWidth("250px");
		invitationsLabel.setText(prefs.getMsg("gadgetLabelPendingInvitations") );
		invitationsLabel.setStylePrimaryName("friendsLabel");
		invitationsContainer.add(invitationsLabel);
		invitationsContainer.add(invitationsList);
	}

	public void viewInvitations() {
		invitationsList.clear();
		viewInvitationsQry.setRequestAction(ReqActionTypes.GET_INVITATIONS);
		viewInvitationsRequest.makeRequest();

	}

	public class ViewInvitationsRequest extends GadgletRequest {
		ViewInvitationsRequest(GadgletQuery req) {
			super(req);
		}

		@Override
		protected void processResults(GadgletResponse jResponse) {

			if (jResponse.isSuccessful()) {

				int inviteNum = jResponse.getRootItemsNum();
				String nickName ;
				

				for (int index = 0; index < inviteNum; index++) {
					JSONObject item = jResponse.getItem(index);
					
					String photo = null;
					
					if(item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName())!=null)
						photo = item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName()).isString().stringValue();
					
					
					if( item.get(ProfileFields.PROFILE_NICKNAME.getParamName()).isString().stringValue().equals(ProfPrivacyStatus.PRIVATE.getPrivacyStatus()))
						nickName = prefs.getMsg("gadgetLabelPrivateProfile") ;
					else
						nickName = item.get(ProfileFields.PROFILE_NICKNAME.getParamName()).isString().stringValue();

					try {
						Invitation invitation = new Invitation(
								jResponse.getItemKeyValue(index), item
										.get(ProfileFields.PROFILE_INVITED_BY
												.getParamName()).isString()
										.stringValue(),nickName, photo);

						invitationsList
								.add(invitation.getInvitationItem());
					} catch (Exception e) {
						debug.showError(111, e.getMessage());
					}
				}

			} else
				debug.showError(101, jResponse.getError());

		}
	}

	public class InvitationsActionRequest extends GadgletRequest {
		InvitationsActionRequest(GadgletQuery req) {
			super(req);
		}

		@Override
		protected void processResults(GadgletResponse jResponse) {

			if (jResponse.isSuccessful()) {
				viewInvitations();
				RemoteFriendsPanel.getRemoteFriendsPanel().viewRemoteFriends();
			} else
				debug.showError(101, jResponse.getError());

		}
	}

	private class Invitation {

		private String invitationId;
		private String invitedBy;
		private String invitedByNickName;
		private String friendPhoto;

		public Invitation(String invitationId, String invitedBy,
				String invitedByNickName, String photo) {

			this.invitationId = invitationId;
			this.invitedBy = invitedBy;
			this.invitedByNickName = invitedByNickName;
			this.friendPhoto = photo;
		}

		public String getInvitationId() {
			return invitationId;
		}

		public String getInvitedBy() {
			return invitedBy;
		}

		public String getInvitedByNickName() {
			return invitedByNickName;
		}

		private Invitation getInvitation() {
			return this;
		}

		HorizontalPanel getInvitationItem() {
			HorizontalPanel item = new HorizontalPanel();
			item.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			item.setHeight("40px");
			

			item.add(new HTML(invitedByNickName));
			item.add(UIUtils.getSpace());
			
			if(this.friendPhoto!=null){
				Image img = new Image(this.friendPhoto);
				img.setPixelSize(30, 30);
					item.add(img);
			}
				
			item.add(UIUtils.getSpace());

			final Button acceptInvitationButton = new Button(
					prefs.getMsg("gadgetLabelAccept"));
			acceptInvitationButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					invitationsActionQry
							.setRequestAction(ReqActionTypes.ACCEPT_FRIEND);
					invitationsActionQry.clearParamList();
					invitationsActionQry.addParam(
							ProfileFields.PROFILE_FRIEND_ID.getParamName(),
							getInvitation().getInvitedBy());
					invitationsActionQry.addParam(
							ProfileFields.PROFILE_INVITATION_ID.getParamName(),
							getInvitation().getInvitationId());

					invitationsActionRequest.makeRequest();


				}
			});

			item.add(acceptInvitationButton);
			item.add(UIUtils.getSpace());

			final Button rejectInvitationButton = new Button(
					prefs.getMsg("gadgetLabelReject"));
			rejectInvitationButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					invitationsActionQry
							.setRequestAction(ReqActionTypes.REJECT_FRIEND);
					invitationsActionQry.clearParamList();
					invitationsActionQry.addParam(
							ProfileFields.PROFILE_FRIEND_ID.getParamName(),
							getInvitation().getInvitedBy());
					invitationsActionQry.addParam(
							ProfileFields.PROFILE_INVITATION_ID.getParamName(),
							getInvitation().getInvitationId());
					invitationsActionRequest.makeRequest();


				}
			});

			item.add(rejectInvitationButton);

			return item;
		}

	}

}
